const utils = require('my-utils')
const db = uniCloud.database();
module.exports = {
	_before: async function() { // 通用预处理器
		this.userInfo = await utils.getUserInfo(this)
		if (!this.userInfo.uid) throw this.userInfo
	},
	async get() {
		return await db.collection('my-mall-brand').get()
	},
	async add() {
		if (!this.userInfo.role.includes('manage')) {
			return {
				msg: '没有权限',
				code: -1
			}
		}
		return await db.collection('my-mall-brand').add(this.params)
	},
	async update() {
		if (!this.userInfo.role.includes('manage')) {
			return {
				msg: '没有权限',
				code: -1
			}
		}
		// 进行深拷贝
		let params = {
			...this.params
		};
		// 删除对象元素
		delete params._id;

		return await db.collection('my-mall-brand').doc(this.params._id).update(params)
	}
}